<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-16
 * Time: 14:04
 */

namespace app\admin\controller;


use app\model\AppSuggest;
use app\util\ReturnCode;

class Suggest extends Base
{

    /**
     * 获取列表
     * @return array
     * @throws \think\exception\DbException
     * @author chniccs
     */
    public function getList()
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $status = $this->request->get('status', null);
        $obj = new AppSuggest();
        $where = ['del' => 0];
        if (isset($status)) {
            $where['status'] = $status;
        }
        $where = $this->addTid($where);
        $obj = $obj->where($where);
        $listObj = $obj->order('create_time DESC')->field('id,content,imgs,create_time,status,uid')->paginate($limit, false, ['page' => $start])->toArray();
        return $this->buildSuccess([
            'list' => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }
    public function getDetail(){
        $id = $this->request->get('id');
        $where = ['id'=>$id,'del'=>0];
        $where = $this->addTid($where);
        $result = AppSuggest::get($where);
        if($result){
            $data = $result->getData();
            $data['userInfo'] = $result->userInfo;
            return $this->buildSuccess($data);
        }
        $this->buildFailed(ReturnCode::FAILED,'获取失败');
    }

    public function del()
    {
        $id = $this->request->get('id');
        $where = ['id' => $id, 'del' => 1];
        $where = $this->addTid($where);
        $affected = AppSuggest::update($where);
        if ($affected) {
            return $this->buildSuccess(null);
        }
        return $this->buildFailed(ReturnCode::FAILED, '删除失败');
    }

    public function changeStatus()
    {
        $id = $this->request->get('id');
        $status = $this->request->get('status');
        $where = ['id' => $id, 'status' => $status];
        $where = $this->addTid($where);
        $affected = AppSuggest::update($where);
        if ($affected) {
            return $this->buildSuccess(null);
        }
        return $this->buildFailed(ReturnCode::FAILED, '操作失败');
    }
}